package com.soft.leo.oldbook.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.soft.leo.oldbook.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Leo on 2017/8/27.
 */

public class MyAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, Object>> data;
    /**
     * LayoutInflater 类是代码实现中获取布局文件的主要形式
     * LayoutInflater layoutInflater = LayoutInflater.from(context);
     * View convertView = layoutInflater.inflate();
     * LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
     * 不同点是LayoutInflater是用来找layout下xml布局文件，并且实例化！
     * 而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
     */
    private LayoutInflater layoutInflater;
    private Context context;

    public MyAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
        this.data = data;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ZuJian zuJian = null;
        if (view == null) {

            view = layoutInflater.inflate(R.layout.booklist_item, null);

        }
        zuJian = new ZuJian();
        zuJian.author = (TextView) view.findViewById(R.id.writer);
        zuJian.bookimg = (ImageView) view.findViewById(R.id.bookpis);
        zuJian.bookname = (TextView) view.findViewById(R.id.bookname);
        zuJian.bookRate = (RatingBar) view.findViewById(R.id.bookrate);
        zuJian.bookRaterNum = (TextView) view.findViewById(R.id.commentNum);
        zuJian.price = (TextView) view.findViewById(R.id.bookprice);
        zuJian.pubDate = (TextView) view.findViewById(R.id.booktime);
        zuJian.publisher = (TextView) view.findViewById(R.id.bookpublisher);
        URL url = null;
        try {
            url = new URL("https://img3.doubanio.com/mpic/s29522132.jpg");
            zuJian.bookimg.setImageBitmap(BitmapFactory.decodeStream(url.openStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }


        zuJian.author.setText((String)(data.get(i).get("author")));
        zuJian.bookname.setText((String)(data.get(i).get("name")));
        zuJian.bookRate.setRating(Float.parseFloat((String)(data.get(i).get("rating"))));
        zuJian.bookRaterNum.setText((String)(data.get(i).get("rater"))+"条评论");
        zuJian.price.setText("￥"+(String)(data.get(i).get("price")));
        zuJian.pubDate.setText((String)(data.get(i).get("pubdate")));
        zuJian.publisher.setText((String)(data.get(i).get("publisher")));


        return view;
    }
    public Bitmap returnBitmap(String url){

        Bitmap mBitmap = null;

        URL urlr = null;
        try {
            urlr = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlr.openConnection();

            InputStream is = conn.getInputStream();

            mBitmap = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
return mBitmap;

    }

}
